package com.example.smartcanteen.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.smartcanteen.mapper.ShopMapper;
import com.example.smartcanteen.model.Shop;
import com.example.smartcanteen.utils.JwtUtil;
import com.example.smartcanteen.utils.ResponseMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ShopService {
    @Autowired
    private ShopMapper shopMapper;


    //创建商家功能
    public ResponseMessage create(Shop shop) {
        ResponseMessage responseMessage = new ResponseMessage();

        //查看是否有重复账号或地址
        QueryWrapper<Shop> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", shop.getUsername())
                .or().eq("address", shop.getAddress());
        if (shopMapper.selectOne(queryWrapper) == null) {
            shopMapper.insert(shop);
            responseMessage.setSuccess(true);
        } else {
            responseMessage.setSuccess(false);
        }
        return responseMessage;
    }


    //商家登录功能
    public ResponseMessage login(String username,String password) {
        ResponseMessage responseMessage = new ResponseMessage();

        //判断账号、密码是否一致
        QueryWrapper<Shop> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", username)
                .eq("password", password);
        responseMessage.setData(shopMapper.selectList(queryWrapper));
        Boolean success = shopMapper.selectOne(queryWrapper) != null;

//        生成token
//        Map<String, Object> claims = new HashMap<>();
//        String token = JwtUtil.genToken(claims);
//        responseMessage.setToken(token);

        responseMessage.setSuccess(success);
        return responseMessage;
    }


    //查看所以商家
    public ResponseMessage selectAll() {
        ResponseMessage responseMessage = new ResponseMessage();
        responseMessage.setData(shopMapper.selectList(null));
        return responseMessage;
    }


    //查看所有营业商家
    public ResponseMessage selectAllUp() {
        ResponseMessage responseMessage = new ResponseMessage();
        QueryWrapper<Shop> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("state","营业中");
        responseMessage.setData(shopMapper.selectList(queryWrapper));
        return responseMessage;
    }


    //搜索指定商家
    public ResponseMessage selectById(String shopID){
        ResponseMessage responseMessage = new ResponseMessage();
        QueryWrapper<Shop> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",shopID);
        responseMessage.setData(shopMapper.selectList(queryWrapper));
        return responseMessage;
    }
}
